package jp.kentaro.plugin.lwjgl.samples.test5.views;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.util.glu.GLU;

public class LwjglCanvas {
	private Texture  m_texture;
	private Display	 m_display;
	private GLCanvas m_context = null;
	private boolean          m_ortho;
	private int m_width;
	private int m_height;
	
	LwjglCanvas(Composite parent, int width, int height){
		GLData data = new GLData ();
		data.doubleBuffer = true;
		m_ortho   = false;
		m_context = new GLCanvas(parent, SWT.NONE, data);
		m_display = parent.getDisplay();
		m_context.setLayout(new FillLayout());
		m_context.setCurrent();
		try {
			GLContext.useContext(m_context);
		} catch(LWJGLException e){
			 e.printStackTrace();
		}
	}
	
	
	
	/*
	 * Set m_ortho to use 2D Graphics
	 */
	public void setOrtho() {
		m_ortho = true;
	}

	public void makeCurrent() {
		m_context.setCurrent();
		try {
			GLContext.useContext(m_context);
		} catch(LWJGLException e){
			 e.printStackTrace();
		}
	}
	
	private static void init(){
		 GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		 GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
		 GL11.glClearDepth(1.0);
		 GL11.glLineWidth(2);
		 GL11.glEnable(GL11.GL_DEPTH_TEST);
	 }
	
	private void reshape() {
		 Rectangle bounds = m_context.getBounds();
		 m_height = bounds.height;
		 m_width  = bounds.width;
		 
		 m_context.setCurrent();
		 try {
			 GLContext.useContext(m_context);
		 } catch(LWJGLException e) { e.printStackTrace(); }
		
		 GL11.glViewport(0, 0, bounds.width, bounds.height);
		 GL11.glMatrixMode(GL11.GL_PROJECTION);
		 GL11.glLoadIdentity();
		 if(m_ortho) {
			 GL11.glOrtho(0.0, bounds.width, bounds.height, 0.0, -1.0, 1.0);
		 } else {
			 float fAspect = (float) bounds.width / (float) bounds.height;
			 GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
		 }
		 GL11.glMatrixMode(GL11.GL_MODELVIEW);
		 GL11.glLoadIdentity();
	 }
	
	 private void draw(){
		 if (!m_context.isDisposed()) {
			 m_context.setCurrent();
				try {
					GLContext.useContext(m_context);
				} catch(LWJGLException e) { e.printStackTrace(); }
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
				GL11.glLoadIdentity();
				render();
				m_context.swapBuffers();
			}
	 }
	 
	 private void render(){
		 m_texture.bind();
		 			 
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
			//GL11.glColor3f(1.0f, 0.0f, 0.0f);
			GL11.glBegin(GL11.GL_QUADS);
				GL11.glTexCoord2f(0.0f, 0.0f);
				GL11.glVertex3i(0, 0, 0);
				
				GL11.glTexCoord2f(1.0f, 0.0f);
				GL11.glVertex3i(m_width, 0, 0);
				
				GL11.glTexCoord2f(1.0f, 1.0f);
				GL11.glVertex3i(m_width, m_height, 0);
				
				GL11.glTexCoord2f(0.0f, 1.0f);
				GL11.glVertex3i(0, m_height, 0);	
			GL11.glEnd();
		}
	 public InputStream getInputStream(String resource) {
			
			InputStream stream = this.getClass().getResourceAsStream(resource);
			
			if (stream == null)  stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
			
			if (stream == null) {
				File file = new File(resource);
				try {
					return new FileInputStream(file);
				} catch (Exception ex) {
					//
				}
				
				try {
					URL sourceUrl = new URL(resource);
					if (sourceUrl != null) {
						stream = sourceUrl.openStream();
					}
				} catch (Exception e) {
					return null;
				}
			}
			return stream;
		}
	 
	 public void start() throws FileNotFoundException {
			//renderer = new Renderer(display, canvas);
		 	m_context.addListener(SWT.Resize, new Listener() {
				public void handleEvent(Event event) {
					reshape();
				}
			});
		 
			 init();
			// Enable Texture
			GL11.glEnable(GL11.GL_TEXTURE_2D);

			
			 m_display.asyncExec(new Runnable() {
				 int index = 0;
					public void run() {
						if (!m_context.isDisposed()) {
							m_context.setCurrent();
							try {
								GLContext.useContext(m_context);
							} catch(LWJGLException e) { e.printStackTrace(); }
							//renderer.display();
							String filename = "texture/r_pav_0_99/r_pav4_2_" + index + ".jpg";
							TextureLoader loader = new TextureLoader();
							m_texture = loader.loadTexture(filename, false);
							
							render();
							m_context.swapBuffers();
							index++;
							if(index == 100) index = 0;
							m_display.asyncExec(this);
						}
					}
				});
			 
		}
}
